package cn.lanqiao.ui;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import cn.lanqiao.entity.Student;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;
public class UpdateStudent extends JDialog {

	private static final long serialVersionUID = 6343338366785534993L;
	private JTextField txtSid;
	private JTextField txtSname;
	private JTextField txtSgender;
	private JTextField txtScollege;
	private JTextField txtSmajor;
	private JTextField txtSclass;
	private JTable table;
	public static void main(String[] args) {
		try {
			UpdateStudent dialog = new UpdateStudent();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public UpdateStudent(Student student,JTable table) {
		this();
		this.txtSid.setText(student.getSid());
		this.txtSname.setText(student.getSname());
		this.txtSgender.setText(student.getSgender());
		this.txtScollege.setText(student.getScollege());
		this.txtSmajor.setText(student.getSmajor());
		this.txtSclass.setText(student.getSclass());
		this.table = table;
	}
	public UpdateStudent() {
		setTitle("学籍录入");
		setResizable(false);
		setBounds(100, 100, 368, 388);
		this.setLocationRelativeTo(null);
		getContentPane().setLayout(null);
		
		JLabel label = new JLabel("学号");
		label.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		label.setBounds(61, 45, 72, 18);
		getContentPane().add(label);
		
		txtSid = new JTextField();
		txtSid.setEnabled(false);
		txtSid.setBounds(134, 42, 86, 24);
		getContentPane().add(txtSid);
		txtSid.setColumns(10);
		
		JLabel label_1 = new JLabel("姓名");
		label_1.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		label_1.setBounds(61, 76, 72, 18);
		getContentPane().add(label_1);
		
		txtSname = new JTextField();
		txtSname.setBounds(134, 73, 86, 24);
		getContentPane().add(txtSname);
		txtSname.setColumns(10);
		
		JLabel label_2 = new JLabel("性别");
		label_2.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		label_2.setBounds(61, 107, 72, 18);
		getContentPane().add(label_2);
		
		txtSgender = new JTextField();
		txtSgender.setBounds(134, 104, 86, 24);
		getContentPane().add(txtSgender);
		txtSgender.setColumns(10);
		
		JLabel label_3 = new JLabel("学院");
		label_3.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		label_3.setBounds(61, 135, 72, 18);
		getContentPane().add(label_3);
		
		txtScollege = new JTextField();
		txtScollege.setBounds(134, 132, 131, 24);
		getContentPane().add(txtScollege);
		txtScollege.setColumns(10);
		
		JLabel label_4 = new JLabel("专业");
		label_4.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		label_4.setBounds(61, 166, 72, 18);
		getContentPane().add(label_4);
		
		txtSmajor = new JTextField();
		txtSmajor.setBounds(134, 163, 86, 24);
		getContentPane().add(txtSmajor);
		txtSmajor.setColumns(10);
		
		JLabel label_5 = new JLabel("班级");
		label_5.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		label_5.setBounds(61, 201, 72, 18);
		getContentPane().add(label_5);
		
		txtSclass = new JTextField();
		txtSclass.setBounds(134, 197, 86, 24);
		getContentPane().add(txtSclass);
		txtSclass.setColumns(10);
		
		JButton Cancel = new JButton("取消");
		Cancel.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		Cancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//取消
				UpdateStudent.this.dispose();
			}
		});
		Cancel.setBounds(28, 272, 113, 27);
		getContentPane().add(Cancel);
		
		JButton SaveUpdate = new JButton("确认修改");
		SaveUpdate.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		SaveUpdate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//确认修改
				//一、取文本框数据
				String sid = txtSid.getText();
				String sname = txtSname.getText();
				String sgender = txtSgender.getText();
				String scollege = txtScollege.getText();
				String smajor = txtSmajor.getText();
				String sclass = txtSclass.getText();
				//二、封装成一个对象
				Student student = new Student(sid,sname,sgender,scollege,smajor,sclass);
				//三、调业务层
				cn.lanqiao.service.UserService update = new cn.lanqiao.service.impl.UserServiceImpl();
				update.updateUsers(sid, student);
				//关闭本对话框
				UpdateStudent.this.dispose();
				Object[][] dates = update.lists();
				String [] header=new String[] {"学号","姓名","性别","学院","专业","班级","入学时间"};
				table.setModel(new DefaultTableModel(dates,header));
				table.getColumnModel().getColumn(2).setPreferredWidth(47);
				table.getColumnModel().getColumn(3).setPreferredWidth(147);
				table.getColumnModel().getColumn(5).setPreferredWidth(48);
			}
		});
		SaveUpdate.setBounds(206, 272, 113, 27);
		getContentPane().add(SaveUpdate);
	}
}
